package com.info1kclient;

import org.eclipse.paho.client.mqttv3.MqttException;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.Menu;

public class MainActivity extends Activity {

    public static final String PREFS_NAME = "Sub_Topics";
	SharedPreferences topicStorage;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	    
		// Construct a instance of SharedPreferences to store the topics
		topicStorage = getSharedPreferences(PREFS_NAME, 0);
		SharedPreferences.Editor editorTopicStorage = topicStorage.edit();
		// Save topic-QoS pair as a preference
		editorTopicStorage.putInt("discount/supermarket", 0);
		editorTopicStorage.putInt("game/sanguosha", 0);
		editorTopicStorage.commit();
		
		// start service to perform MQTT subcription   
		startService(new Intent(this, MqttService.class));
		
		// test publishing function
		String msgPublish = "Hello from Android client";
		try {
			new MqttService().publish("Sample/Java/v3", 0, msgPublish.getBytes());
		} catch (MqttException e) {
			e.printStackTrace();
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
}
